﻿#ifndef MQTT_CONV_H
#define MQTT_CONV_H

#include "mosquitto.h"
#include "mosquittopp.h"

#include <QThread>

class MqttWrapper: public QThread, public mosqpp::mosquittopp
{
    Q_OBJECT

public:
    explicit MqttWrapper(const char *id, bool cleanSession=true);
    virtual ~MqttWrapper();

    void setAddress(const QString &url, int port);

    virtual void on_connect(int rc) override;
    virtual void on_disconnect(int rc) override;
    virtual void on_publish(int mid) override;
    virtual void on_message(const struct mosquitto_message *message) override;
    virtual void on_subscribe(int mid, int qos_count, const int *granted_qos) override;
    virtual void on_unsubscribe(int mid) override;
    virtual void on_log(int level, const char *str) override;
    virtual void on_error() override;

    void mqttDisconnect();
    void qobjectDisconnect();

    void reducePublish(const char *topic,const QByteArray &data);

signals:
    void textMessageReceived(const QString &topic, const QByteArray &textMessage);
    void connected();
    void disconnected();
    void error();

protected:
    virtual void run() override;

protected:
    QString m_id;
    QString token;
    QString url;
    int     port;
};

#endif
